package br.unifacs.mda.plugin.transformation.uml2java;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.m2m.atl.drivers.emf4atl.ASMEMFModel;
//import org.eclipse.m2m.atl.engine.AtlEMFModelHandler;
//import org.eclipse.m2m.atl.engine.AtlLauncher;
//import org.eclipse.m2m.atl.engine.AtlModelHandler;
import org.eclipse.osgi.baseadaptor.loader.ClasspathEntry;

public class ATLTransformUML2JAVA {

	private static ATLTransformUML2JAVA instance = null;

//	private AtlEMFModelHandler modelHandler;
	private URL PIM_ModelResource;
	private URL PSMJAVA_ModelResource;
	private URL PIM2PSMJAVA_TransfoResource;
	private ASMEMFModel pimMetamodel;
	private ASMEMFModel psmJavaMetamodel;

	static public ATLTransformUML2JAVA getInstance() {
		if (instance == null)
			instance = new ATLTransformUML2JAVA();
		return instance;
	}

	private void createResources() {
//		modelHandler = (AtlEMFModelHandler) AtlModelHandler
//				.getDefault(AtlModelHandler.AMH_EMF);
		
		ClassLoader loader = ATLTransformUML2JAVA.class.getClassLoader();
		PIM_ModelResource = loader.getResource("/metamodels/pim.ecore");
		PSMJAVA_ModelResource = loader.getResource("/metamodels/psmJava.ecore");
		
		PIM2PSMJAVA_TransfoResource = ATLTransformUML2JAVA.class
				.getResource("UML2Java.asm");
	}

	private void initMetamodels(Map<String, Object> models) {
//		try {
//			pimMetamodel = (ASMEMFModel) modelHandler.loadModel("PIM",
//					modelHandler.getMof(), PIM_ModelResource.openStream());
//			psmJavaMetamodel = (ASMEMFModel) modelHandler.loadModel("PSMJAVA",
//					modelHandler.getMof(), PSMJAVA_ModelResource.openStream());
//		} catch (IOException e) {
//			e.printStackTrace();
//		}

		models.put("PIM", pimMetamodel);
		models.put("PSMJAVA", psmJavaMetamodel);

	}

	public void pim2psm(String inFilePath, String outFilePath) {
		try {
			Map<String, Object> models = new HashMap<String, Object>();
			createResources();
			initMetamodels(models);

//			// get/create models
//			ASMEMFModel famInputModel = (ASMEMFModel) modelHandler.loadModel(
//					"IN", pimMetamodel, URI.createFileURI(inFilePath));
//			ASMEMFModel perOutputModel = (ASMEMFModel) modelHandler.newModel(
//					"OUT", URI.createFileURI(outFilePath).toFileString(),
//					psmJavaMetamodel);
//			// load models
//			models.put("IN", famInputModel);
//			models.put("OUT", perOutputModel);
//			// launch
//			AtlLauncher.getDefault().launch(this.PIM2PSMJAVA_TransfoResource,
//					Collections.EMPTY_MAP, models, Collections.EMPTY_MAP,
//					Collections.EMPTY_LIST, Collections.EMPTY_MAP);
//
//			modelHandler.saveModel(perOutputModel, outFilePath, false);
//			dispose(models);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	private void dispose(Map<String, Object> models) {
//		for (Object model : models.values())
//			((ASMEMFModel) model).dispose();
	}

}
